﻿using System;
using System.Linq;
using System.ServiceModel.Activation;
using Microsoft.Commerce.Common.MessageBuilders;
using Microsoft.Commerce.Contracts;
using Microsoft.Commerce.Contracts.Messages;
using MusicWorks.Service.DataContracts;
using MusicWorks.Service.Enums;
using MusicWorks.Service.Contracts;

namespace MusicWorks.Service
{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
    public class ShoppingService : BaseServiceClass, IShoppingService
    {
        /// <summary>
        /// Adds to cart.
        /// </summary>
        /// <param name="productId">The product id.</param>
        /// <param name="variantId">The variant id.</param>
        /// <param name="quantity">The quantity.</param>
        /// <param name="catalog">The catalog.</param>
        public Basket AddToBasket(Guid userId, string basketName, string productId, string variantId, int quantity, string catalog)
        {
            //TODO: Figure out why this doesn't actually work... A basket is returned, but no items are ever added to it.

            CommerceEntity basket = null;
            if (!string.IsNullOrEmpty(productId) && quantity > 0 && !string.IsNullOrEmpty(catalog))
            {
                if (String.IsNullOrEmpty(variantId))
                {
                    variantId = null;
                }

                var builder = new CommerceUpdate<Basket, CommerceModelSearch<Basket>>();
                builder.SearchCriteria.Model.UserId = userId.ToString();
                builder.SearchCriteria.Model.Name = basketName;
                builder.SearchCriteria.Model.BasketType = BasketType.Cart;

                //Targeting context required for discounts
                //var targetingContext = new TargetingContex();
                //targetingContext.ChannelName = "DefaultChannel";
                //builder.SearchCriteria.Model.TargetingContext = new CommerceRelationship();
                //builder.SearchCriteria.Model.TargetingContext.Target = targetingContext.ToCommerceEntity();
                

                var newLineItem = new CommerceCreateRelatedItem<LineItem>(Basket.RelationshipName.LineItems, LineItem.ModelNameDefinition);
                newLineItem.Model.CatalogName = catalog;
                newLineItem.Model.ProductId = productId;
                newLineItem.Model.VariantId = variantId;
                newLineItem.Model.Quantity = quantity;

                builder.RelatedOperations.Add(newLineItem);
                builder.UpdateOptions.ReturnModel = new Basket();
                var lineItemQuery = new CommerceQueryRelatedItem<LineItem>(Basket.RelationshipName.LineItems);
                builder.UpdateOptions.ReturnModelQueries.Add(lineItemQuery);

                Broker.AddRequest(builder);
                Broker.Execute();

                CommerceUpdateOperationResponse response = (CommerceUpdateOperationResponse)Broker.Responses.First();
                basket = response.CommerceEntities.FirstOrDefault();
            }

            return basket;
        }


        public Basket GetBasket(Guid userId, string basketName)
        {
            var basketQuery = new CommerceQuery<Basket, CommerceModelSearch<Basket>>();
            basketQuery.SearchCriteria.Model.Name = basketName;
            basketQuery.SearchCriteria.Model.BasketType = BasketType.Cart;
            basketQuery.SearchCriteria.Model.UserId = userId.ToString();
            
            Broker.AddRequest(basketQuery);
            Broker.Execute();

            CommerceQueryOperationResponse response = (CommerceQueryOperationResponse)Broker.Responses.FirstOrDefault();

            return response.CommerceEntities.FirstOrDefault();
        }

    }
}

      


       
    

